const http = require("http");
const url = require("url");
const fs = require("fs");
const server = http.createServer((req, res) => {
  let query = url.parse(req.url, true).query;
  if (typeof query.username !== "undefined") {
    try {
      if (!fs.existsSync("./data.json")) {
        fs.writeFileSync("./data.json", JSON.stringify([]));
      }
      let arr = JSON.parse(fs.readFileSync("./data.json"));
      arr.push(query);

      fs.writeFileSync("./data.json", JSON.stringify(arr));

      res.write("success");
    } catch (err) {
      console.log(err);
      res.write("error");
    }
  }
  res.end();
});
server.listen(8990);
